<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/26 0026
// +----------------------------------------------------------------------
// | Description:  
// +----------------------------------------------------------------------

namespace app\common\model;

use app\common\traits\JumpReturn;
use think\Model;

class AdminBaseModel extends Model
{

    use JumpReturn;

    protected $autoWriteTimestamp="int";
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';


    /**
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @name: init
     * @describe:
     */
    protected static function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
    }


    /**
     * @return int
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/6/26 0026
     * @describe:
     */
    public function setCreateTimeAttr(){
        return time();
    }


    /**
     * @return int
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/6/26 0026
     * @describe:
     */
    public function setUpdateTimeAttr(){
        return time();
    }


    /**
     * @param $time
     * @return false|string
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/6/27 0027
     * @describe:
     */
    protected function getCreateTimeAttr($time){

        return date("Y-m-d H:i:s",$time);
    }


    /**
     * @param $data
     * @return \think\response\Json
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/6/28 0028
     * @describe:添加编辑操作
     */
    public function doAllData($data){
        try {
            if (isset($data['id'])){
                $result= self::update($data);
            }else{
                $result= self::create($data);
            }
            if ($result){
                return self::JsonReturn("更新添加成功");
            }else{
                return self::JsonReturn("更新添加失败",0);
            }
        }catch (\Exception $e){
            return self::JsonReturn($e->getMessage(),0);
        }
    }


    /**
     * @param $ids
     * @return \think\response\Json
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/6/28 0028
     * @describe:公共删除方法
     */
    public function del($ids){
        try {
            if (is_array($ids)){
                $id=$ids;
            }else{
                $id=@explode(",",$ids);
            }
            $result = self::where('id', 'in',$id)->delete();

            if ($result) {
                return self::JsonReturn("删除成功");
            } else {
                return self::JsonReturn("删除失败",0);
            }
        } catch (\Exception $exception) {
            return self::JsonReturn($exception->getMessage(),0);
        }
    }


}